﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2012 ,  TECH, Ltd. 
//--------------------------------------------------------------------

using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.ServiceModel;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
	using RDIFramework.Utilities;
    using RDIFramework.IService;

	/// <summary>
	/// StaffService
    /// 员工服务
	/// 
	/// 修改紀錄
	/// 
	///		2012-03-02 版本：1.0 EricHu 建立檔案。
	///		
	/// 版本：1.0
	///
	/// <author>
	///		<name>EricHu</name>
	///		<date>2012-03-02</date>
	/// </author> 
	/// </summary>
	[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
	public class StaffService : System.MarshalByRefObject, IStaffService
	{
        private string serviceName = RDIFrameworkMessage.StaffService;
		
		/// <summary>
        /// .NET快速开发整合框架（RDIFramework.NET）数据库连接
		/// </summary>
        private readonly string RDIFrameworkDbConection = SystemInfo.RDIFrameworkDbConection;

        #region public string Add(UserInfo userInfo, PiStaffEntity entity, out string statusCode, out string statusMessage, string organizeId = "") 添加员工
        /// <summary>
        /// 添加员工
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="entity">实体</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回消息</param>
        /// <param name="organizeId">组织机构主键</param>
		/// <returns>主鍵</returns>
        public string Add(UserInfo userInfo, PiStaffEntity entity, out string statusCode, out string statusMessage, string organizeId = "")
		{
			// 写入调试信息
			#if (DEBUG)
				int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
			#endif

			// 加强安全验证防止未授权匿名调用
			#if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
			#endif

			string returnValue = string.Empty;            
			statusCode = string.Empty;            
			using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					dbProvider.Open(RDIFrameworkDbConection);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.StaffService_AddStaff, MethodBase.GetCurrentMethod());
					PiStaffManager manager = new PiStaffManager(dbProvider, userInfo);
					returnValue = manager.Add(entity, out statusCode);                   
					statusMessage = manager.GetStateMessage(statusCode);

                    if (returnValue.Length > 0 && statusCode == StatusCode.OKAdd.ToString() && !string.IsNullOrEmpty(organizeId.Trim()))
                    {
                        
                        PiStaffOrganizeEntity staffOrganizeEntity = new PiStaffOrganizeEntity();
                        staffOrganizeEntity.StaffId = BusinessLogic.ConvertToInt32(returnValue);
                        staffOrganizeEntity.OrganizeId = BusinessLogic.ConvertToInt32(organizeId);
                        staffOrganizeEntity.Enabled = 1;
                        staffOrganizeEntity.DeleteMark = 0;
                        PiStaffOrganizeManager staffOrganizeManager = new PiStaffOrganizeManager(dbProvider, userInfo);
                        staffOrganizeManager.Add(staffOrganizeEntity);
                    }
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(dbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					dbProvider.Close();
				}			
			}

			// 写入调试信息
			#if (DEBUG)
				BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
			#endif
			
			return returnValue;
		}
        #endregion

        #region public DataTable GetDT(UserInfo userInfo) 获取员工列表
        /// <summary>
        /// 获取员工列表
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <returns>数据表</returns>
		public DataTable GetDT(UserInfo userInfo)
		{
			// 写入调试信息
			#if (DEBUG)
				int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
			#endif

			// 加强安全验证防止未授权匿名调用
			#if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
			#endif

			DataTable dataTable = new DataTable(PiStaffTable.TableName);

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					dbProvider.Open(RDIFrameworkDbConection);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.StaffService_GetDT, MethodBase.GetCurrentMethod());
					// 取得列表
					PiStaffManager manager = new PiStaffManager(dbProvider, userInfo);
					dataTable = manager.GetDT(PiStaffTable.FieldDeleteMark, 0, PiStaffTable.FieldSortCode);
					dataTable.TableName = PiStaffTable.TableName;
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(dbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					dbProvider.Close();
				}					
			}

			// 写入调试信息
			#if (DEBUG)
				BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
			#endif

			return dataTable;
		}
        #endregion

        #region public DataTable GetDTByPage(UserInfo userInfo,out int recordCount,int pageIndex=1,int pageSize=20,string whereConditional = "",string order = "") 获取员工分页列表
        /// <summary>
        /// 获取员工分页列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="recordCount">所有员工数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">分页大小（默认20条）</param>
        /// <param name="whereConditional">条件表达式</param>
        /// <param name="order">排序字段</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByPage(UserInfo userInfo,out int recordCount,int pageIndex=1,int pageSize=20,string whereConditional = "",string order = "")
        {
            // 写入调试信息
            #if (DEBUG)
				int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
            LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(PiStaffTable.TableName);

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.StaffService_GetDT, MethodBase.GetCurrentMethod());
                    // 取得列表
                    PiStaffManager manager = new PiStaffManager(dbProvider, userInfo);
                    if (string.IsNullOrEmpty(whereConditional))
                    {
                        whereConditional = PiStaffTable.FieldDeleteMark + " = 0 ";
                    }
                    else
                    {
                        whereConditional += " AND " + PiStaffTable.FieldDeleteMark + " = 0 ";
                    }

                    dataTable = manager.GetDTByPage(out recordCount, pageIndex, pageSize, whereConditional, order);
                    dataTable.TableName = PiStaffTable.TableName;
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

                // 写入调试信息
                #if (DEBUG)
				BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
                #endif

            return dataTable;
        }
        #endregion

        #region public PiStaffEntity GetEntity(UserInfo userInfo, string id) 获取实体
        /// <summary>
        /// 获取实体
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="id">主鍵</param>
        /// <returns>实体</returns>
		public PiStaffEntity GetEntity(UserInfo userInfo, string id)
		{
			// 写入调试信息
			#if (DEBUG)
				int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
			#endif

			// 加强安全验证防止未授权匿名调用
			#if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
			#endif

			PiStaffEntity entity = null;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					dbProvider.Open(RDIFrameworkDbConection);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.StaffService_GetEntity, MethodBase.GetCurrentMethod());
					PiStaffManager manager = new PiStaffManager(dbProvider, userInfo);
					entity = manager.GetEntity(id);
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(dbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					dbProvider.Close();
				}			
			}

			// 写入调试信息
			#if (DEBUG)
				BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
			#endif

			return entity;
		}
        #endregion

        #region public int UpdateStaff(UserInfo userInfo, PiStaffEntity entity, out string statusCode, out string statusMessage) 更新员工
        /// <summary>
        /// 更新员工
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="entity">实体</param>
		/// <param name="statusCode">返回状态码</param>
		/// <param name="statusMessage">返回状态信息</param>
        /// <returns>影响行数</returns>
        public int UpdateStaff(UserInfo userInfo, PiStaffEntity entity, out string statusCode, out string statusMessage)
		{
			// 写入调试信息
			#if (DEBUG)
				int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
			#endif

			// 加强安全验证防止未授权匿名调用
			#if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
			#endif

			int returnValue = 0;
			statusCode = string.Empty;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
                    dbProvider.Open(RDIFrameworkDbConection);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.StaffService_UpdateStaff, MethodBase.GetCurrentMethod());
					PiStaffManager manager = new PiStaffManager(dbProvider, userInfo);
					returnValue = manager.Update(entity, out statusCode);
					statusMessage = manager.GetStateMessage(statusCode);
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(dbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					dbProvider.Close();
				}			
			}

			// 写入调试信息
			#if (DEBUG)
				BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
			#endif

			return returnValue;
		}
        #endregion

        #region public DataTable GetDTByIds(UserInfo userInfo, string[] ids) 获取员工列表
        /// <summary>
        /// 获取员工列表
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主鍵</param>
		/// <returns>数据表</returns>
		public DataTable GetDTByIds(UserInfo userInfo, string[] ids)
		{
			// 写入调试信息
			#if (DEBUG)
				int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
			#endif

			// 加强安全验证防止未授权匿名调用
			#if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
			#endif

			DataTable dataTable = new DataTable(PiStaffTable.TableName);

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					dbProvider.Open(RDIFrameworkDbConection);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.StaffService_GetDTByIds, MethodBase.GetCurrentMethod());
					PiStaffManager manager = new PiStaffManager(dbProvider, userInfo);
					dataTable = manager.GetDT(PiStaffTable.FieldId, ids, PiStaffTable.FieldSortCode);
					dataTable.TableName = PiStaffTable.TableName;
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(dbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					dbProvider.Close();
				}	
			}

			// 写入调试信息
			#if (DEBUG)
				BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
			#endif

			return dataTable;
		}
        #endregion

        #region public DataTable GetDTByOrganize(UserInfo userInfo, string organizeId, bool containChildren) 获得员工列表通过组织机构
        /// <summary>
        /// 获得员工列表通过组织机构
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="organizeId">组织机构主键</param>
        /// <param name="containChildren">含子部门</param>
        /// <returns>数据表</returns>       
        public DataTable GetDTByOrganize(UserInfo userInfo, string organizeId, bool containChildren)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(PiStaffTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 用户已经不存在的需要整理干净，防止数据不完整。
                    string sqlQuery = "UPDATE PiStaff SET UserId = NULL WHERE (UserId NOT IN (SELECT Id FROM PiUser))";
                    dbProvider.ExecuteNonQuery(sqlQuery);
                    PiStaffManager staffManager = new PiStaffManager(dbProvider, userInfo);
                    if (containChildren)
                    {
                        dataTable = staffManager.GetChildrenStaffs(organizeId);
                    }
                    else
                    {
                        dataTable = staffManager.GetDTByOrganize(organizeId);
                    }
                    dataTable.TableName = PiStaffTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.StaffService_GetDTByOrganize, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return dataTable;
        }
        #endregion

        #region public int SetStaffUser(UserInfo userInfo, string staffId, string userId)
        /// <summary>
        /// 员工关联用户
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="staffId">员工主键</param>
        /// <param name="userId">用户主键</param>
        /// <returns>影响行数</returns>
        public int SetStaffUser(UserInfo userInfo, string staffId, string userId)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiStaffManager staffManager = new PiStaffManager(dbProvider, userInfo);
                    if (string.IsNullOrEmpty(userId))
                    {
                        returnValue = staffManager.SetProperty(staffId, PiStaffTable.FieldUserId, userId);
                    }
                    else
                    {
                        // 一个用户只能帮定到一个帐户上，检查是否已经绑定过这个用户了。
                        string[] staffIds = staffManager.GetIds(new KeyValuePair<string, object>(PiStaffTable.FieldUserId, userId), new KeyValuePair<string, object>(PiStaffTable.FieldDeleteMark, 0));
                        if (staffIds == null || staffIds.Length == 0)
                        {
                            returnValue = staffManager.SetProperty(staffId, PiStaffTable.FieldUserId, userId);
                            PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                            PiUserEntity userEntity = userManager.GetEntity(userId);
                            returnValue = staffManager.SetProperty(staffId, PiStaffTable.FieldUserName, userEntity.UserName);
                        }
                    }
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.StaffService_SetStaffUser, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif
            return returnValue;
        }
        #endregion

        #region public int DeleteUser(UserInfo userInfo, string staffId)
        /// <summary>
        /// 删除员工关联的用户
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="staffId">员工主键</param>
        /// <returns>影响行数</returns>
        public int DeleteUser(UserInfo userInfo, string staffId)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiStaffManager staffManager = new PiStaffManager(dbProvider, userInfo);
                    returnValue = staffManager.DeleteUser(staffId);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion

        #region public int Delete(UserInfo userInfo, string id) 单个删除
        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <returns>影响行数</returns>
        public int Delete(UserInfo userInfo, string id)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiStaffManager staffManager = new PiStaffManager(dbProvider, userInfo);
                    returnValue = staffManager.Delete(id);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion

        #region public int BatchDelete(UserInfo userInfo, string[] ids) 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>影响行数</returns>
        public int BatchDelete(UserInfo userInfo, string[] ids)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());

            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiStaffManager staffManager = new PiStaffManager(dbProvider, userInfo);
                    returnValue = staffManager.BatchDelete(ids);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion

        #region public int SetDeleted(UserInfo userInfo, string[] ids) 批量打删除标志
        /// <summary>
        /// 批量打删除标志
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>影响行数</returns>
        public int SetDeleted(UserInfo userInfo, string[] ids)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    PiStaffManager staffManager = new PiStaffManager(dbProvider, userInfo);
                    PiStaffEntity staffEntity = null;
                    for (int i = 0; i < ids.Length; i++)
                    {
                        // 删除相应的用户
                        staffEntity = staffManager.GetEntity(ids[i]);
                        if (staffEntity.UserId != null)
                        {
                            userManager.SetDeleted(staffEntity.UserId);
                        }
                        // 删除职员
                        returnValue += staffManager.SetDeleted(ids[i], true);
                        PiStaffOrganizeManager staffOrganizeManager = new PiStaffOrganizeManager(dbProvider, userInfo);
                        returnValue += staffOrganizeManager.SetDeleted(new string[] { PiStaffOrganizeTable.FieldStaffId}, new string[] { ids[i]});
                        
                    }
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion

        #region public int MoveTo(UserInfo userInfo, string id, string organizeId) 移动数据
        /// <summary>
        /// 移动数据
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">员工主键</param>
        /// <param name="organizeId">组织机构主键</param>
        /// <returns>影响行数</returns>
        public int MoveTo(UserInfo userInfo, string id, string organizeId)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiStaffOrganizeManager manager = new PiStaffOrganizeManager(dbProvider, userInfo);
                    returnValue = manager.SetProperty(PiStaffOrganizeTable.FieldStaffId, id, PiStaffOrganizeTable.FieldOrganizeId, organizeId);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
#if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif

            return returnValue;
        }
        #endregion

        #region public int BatchMoveTo(UserInfo userInfo, string[] ids, string organizeId) 移动数据
        /// <summary>
        /// 批量移动数据
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="organizeIds">组织机构主键数组</param>
        /// <param name="organizeId">父结点主键</param>
        /// <returns>影响行数</returns>
        public int BatchMoveTo(UserInfo userInfo, string[] ids, string organizeId)
        {
            // 写入调试信息
         #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiStaffOrganizeManager manager = new PiStaffOrganizeManager(dbProvider, userInfo);
                    for (int i = 0; i < ids.Length; i++)
                    {
                        returnValue += manager.SetProperty(PiStaffOrganizeTable.FieldStaffId,ids[i], PiStaffOrganizeTable.FieldOrganizeId, organizeId);
                    }
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion
	}
}
